﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataAccessLayer;
using System.Text;

public partial class web_Controls_ctrl_vl_left_vtn_details : System.Web.UI.UserControl
{
    Common _common = null;
    public string mess = "";
    public string idGui = "", idNhan = "", type = "0", idLuu = "", sUrl = "";
    public Boolean chophepgui = false;
    public Boolean hethan = false;
    public DataTable dtt=null;
    public string sPath="";
    protected void Page_Load(object sender, EventArgs e)
    {
		if(Request.Url.ToString().Contains("/web/pages")) sPath="../../";
		if(Request.Url.ToString().Contains("webadmin/pages")) sPath="../../";
        try
        {
            _common = new Common();
            sUrl = Request.Url.ToString();
            if ((Request.Params["id"] != null))
            {
                idNhan = Request.Params["id"].ToString();
                if (Session["NTV_id"] == null || !(Boolean.Parse(Session["NTV_Active"].ToString()))) chophepgui = false;
                else { chophepgui = true; idGui = Session["NTV_id"].ToString(); }
                LoadvtnDetail(Request.Params["id"].ToString(), "");
            }
            else if ((Request.Params["iddn"] != null))
            {
                idNhan = Request.Params["iddn"].ToString();
                if (Session["NTV_id"] == null || !(Boolean.Parse(Session["NTV_Active"].ToString()))) chophepgui = false;
                else { chophepgui = true; idGui = Session["NTV_id"].ToString(); }
                LoadvtnDetail("", Request.Params["iddn"].ToString());
            }
        }
        catch (Exception er)
        {
            mess += "Loi " + er.Message.ToString();
        }
    }
    void loadTuyenDung(string iddn)
    {
        try
        {
            dtt = _common.getDataTable("select ma_ho_so,tieu_de_ho_so,count_view from ViecLamTrong_ho_so_tuyen_dung " +
                " where ma_doanh_nghiep='" + _common.TextFormat(iddn) + "'");
            
        }
        catch { mess=iddn;}
    }
    void LoadvtnDetail(string id, string iddn)
    {
		try{
			if (!string.IsNullOrEmpty(id))
			{
				string _sql = "select * from ViecLamTrong_ho_so_tuyen_dung where ma_ho_so ='" + id + "'";
				DataSet _ds = _common.ExeceuteDataset(_sql);
				if (_ds.Tables[0].Rows.Count > 0)
				{
					if (LoadDoanhnghiep(_ds.Tables[0].Rows[0]["ma_doanh_nghiep"].ToString()))
					{
						if (!IsPostBack)
							updateCountTuyenDung(_ds.Tables[0].Rows[0]["ma_ho_so"].ToString());
						loadTuyenDung(_ds.Tables[0].Rows[0]["ma_doanh_nghiep"].ToString());
	                    
						idNhan = _ds.Tables[0].Rows[0]["ma_doanh_nghiep"].ToString();
						idLuu = _ds.Tables[0].Rows[0]["ma_doanh_nghiep"].ToString() + "_" + _ds.Tables[0].Rows[0]["ma_ho_so"].ToString(); ;
						lbl_matuyendung.Text = _ds.Tables[0].Rows[0]["ma_ho_so"].ToString();
						if (_ds.Tables[0].Rows[0]["luong_tu"].ToString().Trim().Equals("0"))
						{
							lbl_luong.Text = "Thoả thuận";
						}
						else
						{
							lbl_luong.Text = _ds.Tables[0].Rows[0]["luong_tu"].ToString() + " - " + _ds.Tables[0].Rows[0]["luong_den"].ToString();
						}
						lbl_vi_tri_tuyen_dung.Text = _ds.Tables[0].Rows[0]["vi_tri_tuyen_dung"].ToString();
						lbl_ten_tuyendung.Text = _ds.Tables[0].Rows[0]["tieu_de_ho_so"].ToString();
						lbl_mota_congviec.Text = _ds.Tables[0].Rows[0]["mo_ta"].ToString();
						lbl_quyenloi.Text = _ds.Tables[0].Rows[0]["quyen_loi"].ToString();
						lbl_soluong_tuyen.Text = _ds.Tables[0].Rows[0]["so_luong_tuyen"].ToString() + " (người)";
						lbl_thoihan_nophs.Text = "Từ ngày: " + DateTime.Parse(_ds.Tables[0].Rows[0]["ngay_nhan"].ToString()).ToString("dd/MM/yyyy");
						lbl_thoihan_nophs.Text += " - Đến ngày: " + DateTime.Parse(_ds.Tables[0].Rows[0]["ngay_het_han"].ToString()).ToString("dd/MM/yyyy");
						if (DateTime.Now > DateTime.Parse(_ds.Tables[0].Rows[0]["ngay_het_han"].ToString())) hethan = true;
						//lbl_thoihan_nophs.Text = DateTime.Parse(_ds.Tables[0].Rows[0]["ngay_nhan"].ToString()).ToString("dd/MM/yyyy");

						lbl_hoso_baogom.Text += _ds.Tables[0].Rows[0]["yeu_cau_giay_to"].ToString();
						LoadChucdanh(_ds.Tables[0].Rows[0]["ma_chuc_danh"].ToString());
						LoadNoilamviec(_ds.Tables[0].Rows[0]["noi_lam_viec"].ToString());
						LoadChuyenmon(_ds.Tables[0].Rows[0]["ma_ho_so"].ToString());
						LoadKynangmem(_ds.Tables[0].Rows[0]["ma_ho_so"].ToString());
						LoadNghiepvu(_ds.Tables[0].Rows[0]["ma_ho_so"].ToString());
						LoadYecaukhac(_ds.Tables[0].Rows[0]["ma_ho_so"].ToString());
					}
					else
					{
						//Response.Redirect("Default.aspx");
					}
				}
			}
			else if (!string.IsNullOrEmpty(iddn))
			{//Load theo doanh nghiep
				string _sql = "select top 1 * from ViecLamTrong_ho_so_tuyen_dung where kich_hoat=1 and ma_doanh_nghiep ='" + iddn + "'";
				DataSet _ds = _common.ExeceuteDataset(_sql);
				if (_ds.Tables[0].Rows.Count > 0)
				{
					if (LoadDoanhnghiep(_ds.Tables[0].Rows[0]["ma_doanh_nghiep"].ToString()))
					{
						if (!IsPostBack)
							updateCountTuyenDung(_ds.Tables[0].Rows[0]["ma_ho_so"].ToString());
						loadTuyenDung(_ds.Tables[0].Rows[0]["ma_doanh_nghiep"].ToString());
	                    
						idNhan = _ds.Tables[0].Rows[0]["ma_doanh_nghiep"].ToString();
						idLuu = _ds.Tables[0].Rows[0]["ma_doanh_nghiep"].ToString() + "_" + _ds.Tables[0].Rows[0]["ma_ho_so"].ToString(); ;
						lbl_matuyendung.Text = _ds.Tables[0].Rows[0]["ma_ho_so"].ToString();
						if (_ds.Tables[0].Rows[0]["luong_tu"].ToString().Trim().Equals("0"))
						{
							lbl_luong.Text = "Thoả thuận";
						}
						else
						{
							lbl_luong.Text = _ds.Tables[0].Rows[0]["luong_tu"].ToString() + " - " + _ds.Tables[0].Rows[0]["luong_den"].ToString();
						}
						lbl_ten_tuyendung.Text = _ds.Tables[0].Rows[0]["tieu_de_ho_so"].ToString();
						lbl_mota_congviec.Text = _ds.Tables[0].Rows[0]["mo_ta"].ToString();
						lbl_quyenloi.Text = _ds.Tables[0].Rows[0]["quyen_loi"].ToString();
						lbl_soluong_tuyen.Text = _ds.Tables[0].Rows[0]["so_luong_tuyen"].ToString() + " (người)";
						//lbl_thoihan_nophs.Text = DateTime.Parse(_ds.Tables[0].Rows[0]["ngay_nhan"].ToString()).ToString("dd/MM/yyyy");

						lbl_hoso_baogom.Text += _ds.Tables[0].Rows[0]["yeu_cau_giay_to"].ToString();
						LoadChucdanh(_ds.Tables[0].Rows[0]["ma_chuc_danh"].ToString());
						LoadNoilamviec(_ds.Tables[0].Rows[0]["noi_lam_viec"].ToString());
						LoadChuyenmon(_ds.Tables[0].Rows[0]["ma_ho_so"].ToString());
						LoadKynangmem(_ds.Tables[0].Rows[0]["ma_ho_so"].ToString());
						LoadNghiepvu(_ds.Tables[0].Rows[0]["ma_ho_so"].ToString());
						LoadYecaukhac(_ds.Tables[0].Rows[0]["ma_ho_so"].ToString());
					}
					else
					{
						//Response.Redirect("Default.aspx");
					}
				}
				else
				{//loi
					mess = "loi";
				}
			}
		}catch{
			mess="Loi";
		}
    }
    void updateCountTuyenDung(string id)
    {
        try
        {
            _common.execNoneQuery("update ViecLamTrong_ho_so_tuyen_dung set count_view=count_view+1 where ma_ho_so='" + _common.TextFormat(id) + "'");
        }
        catch { }
    }
    void LoadChucdanh(string ma_chucdanh)
    {
        DataSet _ds_hs_bg = _common.ExeceuteDataset("select * from DanhMuc_chuc_danh where ma_chuc_danh = '" + ma_chucdanh + "'");
        if (_ds_hs_bg.Tables[0].Rows.Count > 0)
            lbl_chuc_danh.Text = _ds_hs_bg.Tables[0].Rows[0]["ten_chuc_danh"].ToString();
    }
    void LoadNoilamviec(string ma)
    {
        string[] _ma_dc = ma.Split('#');
        string _ma_ = "";
        if (_ma_dc.Length > 2)
        {
            for (int i = 0; i < _ma_dc.Length; i++)
            {
                if (i < _ma_dc.Length - 2)
                {
                    _ma_ += _ma_dc[i] + ",";
                }
                else
                {
                    _ma_ += _ma_dc[i];
                }
            }
        }
        else _ma_ = _ma_dc[0];
        //lbl_noilamviec.Text = _ma_;
        if (_ma_ != "")
        {
            DataSet _ds_dc = _common.ExeceuteDataset("select * from DanhMuc_dia_chi where ma_dia_chi IN(" + _ma_ + ")");
            if (_ds_dc.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < _ds_dc.Tables[0].Rows.Count; i++)
                {
                    lbl_noilamviec.Text += _ds_dc.Tables[0].Rows[i]["ten_dia_chi"].ToString() + "<br />";
                }
            }
        }
        else
        {
            DataSet _ds_dc = _common.ExeceuteDataset("select * from DanhMuc_dia_chi where ma_dia_chi='" + ma + "'");
            if (_ds_dc.Tables[0].Rows.Count > 0)
                lbl_noilamviec.Text += _ds_dc.Tables[0].Rows[0]["ten_dia_chi"].ToString() + "<br />";
        }
    }
    void LoadChuyenmon(string ma_ho_so)
    {
        string _sql = "select A.ten_chuyen_mon, B.ten_hoc_van from ViecLamTrong_yeu_cau_chuyen_mon as N left join DanhMuc_chuyen_mon as A on N.ma_chuyen_mon = A.ma_chuyen_mon ";
        _sql += " left join DanhMuc_hoc_van as B on N.ma_hoc_van = B.ma_hoc_van where N.ma_ho_so = '" + ma_ho_so + "'";
        DataSet _ds_cm = _common.ExeceuteDataset(_sql);
        if (_ds_cm.Tables[0].Rows.Count > 0)
        {
            lbl_trinhdo.Text = _ds_cm.Tables[0].Rows[0]["ten_hoc_van"].ToString();
            lbl_chuyen_mon.Text = _ds_cm.Tables[0].Rows[0]["ten_chuyen_mon"].ToString();
        }
    }
    void LoadKynangmem(string ma_ho_so)
    {
        DataSet _ds_hs_bg = _common.ExeceuteDataset("select * from yeu_cau_kha_nang_mem as A left join DanhMuc_kha_nang_mem as B on A.ma_kn_mem = B.ma_kn_mem where A.ma_ho_so = '" + ma_ho_so + "'");
        if (_ds_hs_bg.Tables[0].Rows.Count > 0)
            lbl_kynang_mem.Text = _ds_hs_bg.Tables[0].Rows[0]["ten_kn_mem"].ToString();
    }
    void LoadYecaukhac(string ma_ho_so)
    {
        DataSet _ds_yc = _common.ExeceuteDataset("select * from ViecLamTrong_yeu_cau_ve_nguoi where ma_ho_so = '" + ma_ho_so + "'");
        if (_ds_yc.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < _ds_yc.Tables[0].Rows.Count; i++)
            {
                lbl_yc_khac.Text += "<p>";
                lbl_yc_khac.Text += (bool)_ds_yc.Tables[0].Rows[i]["gioi_tinh"] == true ? "Nam " : "Nữ ";
                lbl_yc_khac.Text += " tuổi từ : " + _ds_yc.Tables[0].Rows[i]["tuoi_tu"].ToString();
                lbl_yc_khac.Text += " tuổi đến : " + _ds_yc.Tables[0].Rows[i]["tuoi_den"].ToString();
                lbl_yc_khac.Text += " cao từ : " + _ds_yc.Tables[0].Rows[i]["cao_tu"].ToString();
                lbl_yc_khac.Text += " cao đến : " + _ds_yc.Tables[0].Rows[i]["cao_den"].ToString();
                lbl_yc_khac.Text += " nặng từ : " + _ds_yc.Tables[0].Rows[i]["nang_tu"].ToString();
                lbl_yc_khac.Text += " nặng đến : " + _ds_yc.Tables[0].Rows[i]["nang_den"].ToString();
                lbl_yc_khac.Text += " Số lượng : " + _ds_yc.Tables[0].Rows[i]["so_luong"].ToString();
                lbl_yc_khac.Text += " </p>";
            }
            
        }
    }
    void LoadNghiepvu(string ma_ho_so)
    {
        DataSet _ds_nv = _common.ExeceuteDataset("select B.* from ViecLamTrong_yeu_cau_nghiep_vu as A left join DanhMuc_nghiep_vu as B on A.ma_nghiep_vu = B.ma_nghiep_vu where A.ma_ho_so = '" + ma_ho_so + "'");
        if (_ds_nv.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < _ds_nv.Tables[0].Rows.Count; i++)
            {
                if ((bool)_ds_nv.Tables[0].Rows[i]["tin_hoc"])
                {
                    lbl_yc_tinhoc.Text = _ds_nv.Tables[0].Rows[i]["ten_nghiep_vu"].ToString();
                }
                if ((bool)_ds_nv.Tables[0].Rows[i]["ngoai_ngu"])
                {
                    lbl_yc_ngoai_ngu.Text = _ds_nv.Tables[0].Rows[i]["ten_nghiep_vu"].ToString();
                }
                if ((bool)_ds_nv.Tables[0].Rows[i]["bang_lai"])
                {
                    lbl_yc_banglai.Text = _ds_nv.Tables[0].Rows[i]["ten_nghiep_vu"].ToString();
                }
            }
        }
    }
    bool LoadDoanhnghiep(string ma_doanh_nghiep)
    {
        DataSet _ds_dn = _common.ExeceuteDataset("select * from DoanhNghiep_doanh_nghiep as A left join DanhMuc_so_huu as B on A.ma_so_huu = B.ma_so_huu where A.ma_doanh_nghiep = '" + ma_doanh_nghiep + "'");
        if (_ds_dn.Tables[0].Rows.Count > 0)
        {
            //if ((bool)_ds_dn.Tables[0].Rows[0]["kich_hoat"])
            //{
                lbl_cty_ten.Text = _ds_dn.Tables[0].Rows[0]["ten_doanh_nghiep"].ToString();
                //lbl_cty_namthanhlap.Text = _ds_dn.Tables[0].Rows[0]["nam_thanh_lap"].ToString();
                lbl_cty_sohuu.Text = _ds_dn.Tables[0].Rows[0]["ten_so_huu"].ToString();
                lbl_cty_ten_tat.Text = _ds_dn.Tables[0].Rows[0]["ten_tat"].ToString();
                lbl_cty_tongnhansu.Text = _ds_dn.Tables[0].Rows[0]["so_lao_dong"].ToString();
                lbl_cty_gioithieu.Text = _ds_dn.Tables[0].Rows[0]["ghi_chu"].ToString();
                if (Session["NTV_id"] == null && Session["NTDuser"] == null && Session["UserID"] == null)
                {
                    lbl_diachi.Text = "Bạn không có đủ quyền xem mục này";
                    lbl_dienthoai.Text = "Bạn không có đủ quyền xem mục này";
                    lbl_email.Text = "Bạn không có đủ quyền xem mục này";

                }
                else
                {
                    lbl_diachi.Text = (bool)_ds_dn.Tables[0].Rows[0]["ht_diachi"] == true ? _ds_dn.Tables[0].Rows[0]["dia_chi_chi_tiet"].ToString() + " " + _common.execScalar("select dbo.fcGetDiaChiChiTiet ('" + _ds_dn.Tables[0].Rows[0]["ma_dia_chi"].ToString() + "')") : "Liên hệ với trung tâm";
                    lbl_dienthoai.Text = (bool)_ds_dn.Tables[0].Rows[0]["ht_dienthoai"] == true ? _ds_dn.Tables[0].Rows[0]["dien_thoai"].ToString() : "Liên hệ với trung tâm";
                    lbl_email.Text = (bool)_ds_dn.Tables[0].Rows[0]["ht_email"] == true ? _ds_dn.Tables[0].Rows[0]["email"].ToString() : "Liên hệ với trung tâm";
                }
                //lbl_diachi.Text = (bool)_ds_dn.Tables[0].Rows[0]["ht_diachi"] == true ? _ds_dn.Tables[0].Rows[0]["dia_chi_chi_tiet"].ToString() + " " + _common.execScalar("select dbo.fcGetDiaChiChiTiet ('" + _ds_dn.Tables[0].Rows[0]["ma_dia_chi"].ToString() + "')") : "Liên hệ với trung tâm";
                //lbl_dienthoai.Text = (bool)_ds_dn.Tables[0].Rows[0]["ht_dienthoai"] == true ? _ds_dn.Tables[0].Rows[0]["dien_thoai"].ToString() : "Liên hệ với trung tâm";
                //lbl_email.Text = (bool)_ds_dn.Tables[0].Rows[0]["ht_email"] == true ? _ds_dn.Tables[0].Rows[0]["email"].ToString() : "Liên hệ với trung tâm";
            //}
            //else
            //{
            //    return false;
            //}
        }
        return true;
    }
}